let num = 1;
let num_c = 1;
let id;
let app = getApp();
let url = app.globalData.url;
let totalPage;
let totalPage_c;
let merchantId;
let userId;
let active;
const defHeaders = {
  'Content-Type': 'application/json;charset=UTF-8',
  'Accept': 'application/json',
};
Page({
  data: {
    classNames: "border-bottom",
    color: '#FF7900',
    list: [],
    lists: [],
    display: "",
    displays: "",
    none: "",

  },
  onLoad(options) {
    console.log("传递参数")
    console.log(options)
    var that = this;
    // my.showLoading({
    //   content: '加载中...',
    //   delay: 0,
    // });

    wx.getStorage({
      key: 'merchantId',
      success: function (res) {
        merchantId = res.data.merchantId;
        wx.getStorage({
          key: 'userId',
          success: function (res) {
            userId = res.data.userId;
            console.log(userId);
            that.all();
          }
        })
      }
    })
  
  },
  all() {
    var that = this;
    wx.getStorage({
      key: 'id',
      success: function (res) {
        id = res.data.id;
        wx.request({
          url: url + '/mobileCardTemplate/getConsumeList.do', // 会员消费记录
          method: 'GET',
          data: {
            cardId: id,
            pageNum: num
          },
          headers: {
            defHeaders,
          },
          dataType: "json",
          success: function (res) {
            console.log(res)
            totalPage = res.data.body.totalPage;
            var arr = [];
            var list = res.data.body.slimPayOrderList
            for (var j = 0; j < list.length; j++) {
              list[j].title = list[j].consumeTime;
              list[j].growthBalance = list[j].paidInAmount;
              list[j].growthPoint = list[j].storeName;
              arr.push(list[j]);
            }
            that.setData({
              list: arr,
              none: "none"
            });
            // my.hideLoading();
          },
        });
      },
      fail: function (res) {
        // my.alert({ content: "加载失败" });
      }
    });

  },
  expense(eve) {
    active = eve.currentTarget.dataset.active
    this.all();
    this.setData({
      classNames: "border-bottom",
      className: "",
      color: '#FF7900',
      colors: '',
      display: "block",
      displays: "none",

    })
  },
  //  储值记录
  storedValue(ele) {
    active = ele.currentTarget.dataset.active
    this.save()
    this.setData({
      className: "border-bottom",
      classNames: "",
      colors: '#FF7900',
      color: '',
      display: "none",
      displays: "block",
    })
  },
  // 储值事件
  save() {
    var that = this;
    wx.request({
      url: url + '/mobileCardTemplate/getDepositList.do', // 目标服务器url
      method: 'GET',
      data: {
        pageNum: num_c,
        cardId: id,
        merchantId: merchantId
      },
      headers: {
        defHeaders,
      },
      dataType: "json",
      success: function (res) {
        console.log(888888888888)
        console.log(res)
        totalPage_c = res.data.body.totalPage;
        var arr = [];
        var list = res.data.body.slimCardDepositHistory
        for (var i = 0; i < list.length; i++) {
          list[i].title = list[i].createTime;
          list[i].growthBalance = list[i].amount;
          list[i].growthPoint = list[i].storeName;
          arr.push(list[i]);
        }
        that.setData({
          lists: arr,
        });
      },
    });
  },
  // 下来刷新
  onPullDownRefresh: function(ele) {
    // if(){}
    num++;
    if (totalPage < num) {
      num = 1;
    }
    var that = this;
    that.all(num);
    wx.stopPullDownRefresh()
    if (active == 1) {
      num++;
      if (totalPage < num) {
        num = 1;
      }
      var that = this;
      that.all(num);
      wx.stopPullDownRefresh()
    } else if (active == 2) {
      num_c++;
      if (totalPage_c < num_c) {
        num_c = 1;
      }
      var that = this;
      wx.stopPullDownRefresh()
    }

  },
});